// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:17:52
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Load\GRAVParser.cpp
 */
#include "Import/BDFImport/BulkData/Load/GRAVParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IGRAVService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (GRAV);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/GRAV/GRAV.md>
*/
enum GRAVkey : std::uint8_t
{
    GRAVkey_SID = 2,
    GRAVkey_CID = 3,  // Coordinate system identification number. (Integer ≥ 0; Default = 0)
    GRAVkey_A   = 4,  // Acceleration vector scale factor. (Real)
    GRAVkey_N1  = 5,  // Acceleration vector components measured in coordinate system CID. (Real; at least one Ni ≠ 0.0)
    GRAVkey_N2  = 6,
    GRAVkey_N3  = 7,
    GRAVkey_MB  = 8,  // Indicates whether the CID coordinate system is defined in the main Bulk Data Section (MB = -1) or the partitioned
                      // superelement Bulk Data Section (MB = 0).
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseGRAV (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存GRAV数据的临时结构体对象
    SG::DataStructure::FEM::GRAVData tempData;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempData.m_domainId = 1;

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            // 如果当前条目为空，跳过当前循环并处理下一个条目
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的GRAV字段
        switch (curIDKey)
        {
        case GRAVkey_SID:
            // 解析SID并赋值
            tempData.m_sId = convertToI64 (curStrKey);
            break;
        case GRAVkey_CID:
            // 解析CID并赋值
            tempData.m_cId = convertToI64 (curStrKey);
            break;
        case GRAVkey_A:
            // 解析加速度系数a并赋值
            tempData.m_a = convertToReal (curStrKey);
            break;
        case GRAVkey_N1:
            // 解析第一个方向余弦N1并赋值
            tempData.m_N[0] = convertToReal (curStrKey);
            break;
        case GRAVkey_N2:
            // 解析第二个方向余弦N2并赋值
            tempData.m_N[1] = convertToReal (curStrKey);
            break;
        case GRAVkey_N3:
            // 解析第三个方向余弦N3并赋值
            tempData.m_N[2] = convertToReal (curStrKey);
            break;
        case GRAVkey_MB:
            // 解析MB值并赋值
            tempData.m_MB = convertToI64 (curStrKey);
            break;
        default:
            // 如果条目ID不匹配，跳过处理
            break;
        }

        // 增加条目ID，以便处理下一个条目
        curIDKey++;
    }

    // 将解析后的GRAV数据通过数据库服务工厂的GRAV服务添加到数据库中
    return dbServiceFactory->get<IGRAVService> ()->Add (tempData);
}